Skip to main content

Java Access Modifiers

Banner java icon

🎭 Java Access Modifiers – Who Gets to Peek? πŸ€”β€‹

Java has four access modifiers that control who can snoop around in your classes, variables, methods, and constructors. Think of them as security levels at a top-secret agency! πŸ•΅οΈβ€β™‚οΈ

1. The VIP List – Access Modifiers at a Glance πŸ‘€β€‹

Access ModifierWho Can Peek? πŸ‘€
publicOpen to the world! 🌍
protectedAccessible to package mates and subclass spies! πŸ•΅οΈβ€β™€οΈ
default (package-private)Only package buddies allowed! πŸšͺ
privateOnly the class itself can access. Strictly confidential! πŸ”’

The access levels go from the friendliest (public) to the most exclusive (private):

Note: public > protected > default (package-private) > private


1.1. public – The Social Butterfly πŸ¦‹β€‹

A public class, method, or constructor is available to everyone, everywhere. Think of it like a celebrity – anyone can approach!

public class Data {

private String format;

public String getFormat() {
return this.format;
}

public void setFormat(String format) {
this.format = format;
}
}

Fun Fact​

All fields in an interface are public static final by default! No secrets there! 🀐


1.2. protected – Friends and Family Only πŸ€β€‹

A protected member is visible in the same package and to subclasses outside the package. It's like an exclusive club – your package-mates and family (subclasses) are welcome!

public class Data {

protected void displayMessage() {
System.out.println("Test message");
}
}

πŸ“Œ In the Same Package​

public class Main {
public static void main(String[] args) {
Data data = new Data();
data.displayMessage(); // Works fine πŸŽ‰
}
}

❌ Outside the Package? No Entry​

If you try to access displayMessage() outside its package without inheritance, Java will slam the door shut! πŸšͺ❌

'displayMessage()' has protected access in 'com.example.Data'

βœ… Inheritance to the Rescue​

public class Main extends Data {
public static void main(String[] args) {
Main main = new Main();
main.displayMessage(); // Works! 🎊
}
}

1.3. default (package-private) – The Neighborhood Watch πŸ‘€β€‹

If you don’t specify an access modifier, Java assumes it's 'default' (a.k.a package-private). That means only classes in the same package can access it.

public class Data {
void displayMessage() {
System.out.println("Default Test message");
}
}

❌ Not Allowed Outside the Package​

'displayMessage()' is not public in 'com.example.Data'. Cannot be accessed from outside package.

1.4. private – Top Secret! πŸš«β€‹

A private member is the most restrictive – only accessible within the class. It's like having a personal diary πŸ” – no one else can read it!

public class Data {

private void displayMessage() {
System.out.println("Top Secret Message!");
}
}

Attempting to access it from another class? 🚨 Access Denied! 🚨

'displayMessage()' has private access in 'Data'

2. Levels of Access Control πŸ”‘β€‹

There are two levels of access control in Java:

  1. Class-level access – A class can be public or default (package-private). Top-level classes cannot be private or protected.
  2. Member-level access – Fields, methods, and constructors can use public, protected, default, or private.

πŸ”Ή Local variables and parameters? They cannot have access modifiers – they are automatically private to the method! 🀫


3. Conclusion – Choose Wisely! πŸ§β€‹

Access levels matter in two big ways:

  1. When using external classes – Access levels determine what parts of those classes you can use.
  2. When writing your own classes – You decide what to expose and what to keep private. A well-thought-out access control strategy prevents unintended misuse and errors! πŸš€

Quick Tips πŸ“Œβ€‹

βœ… Use private whenever possible – Only expose what’s necessary! πŸ”’ βœ… Use default for package-scoped access. 🏑 βœ… Use protected for inheritance scenarios. πŸ‘ͺ βœ… Use public when you want everyone to have access. 🌎


πŸŽ‰ Happy Learning! πŸš€